package be.music.tool.practice;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import be.music.tool.comparator.PracticeEventComparator;
import be.music.tool.time.Time;


public class PracticeRoutine {

	private List<PracticeEvent> events;
	
	public PracticeRoutine () {
		events = new ArrayList<PracticeEvent>();
	}

	/**
	 * @return the events
	 */
	public List<PracticeEvent> getEvents() {
		return events;
	}

	/**
	 * @param events the events to set
	 */
	public void setEvents(List<PracticeEvent> events) {
		this.events = events;
		sortPracticeEvents(events);
	}

	
	public PracticeEvent getPracticeEvent(Time time) {
		
		PracticeEvent event = null;
		
		for(PracticeEvent e : events) {
			
			if(e.coversTime(time)) {
				event = e;
				//Stop the loop. Overlapping events not supported.
				//TODO: Fix overlapping events. Adjust start and end times.
				//TODO: Check if CollectionUtils offers better solution
				break;
			}
			
		}
		
		return event;
	}
	
	public void addEvent(PracticeEvent event) {
		this.events.add(event);
		sortPracticeEvents(events);
	}
	
	private void sortPracticeEvents(List<PracticeEvent> events) {
		Collections.sort(events, new PracticeEventComparator());
	}
	
}
